package com.health.mapp.impl;

import java.math.BigDecimal;

import org.apache.commons.lang3.StringUtils;

import com.health.db.model.RegisterOrder;
import com.health.db.model.RegisterOrderZProcess;
import com.health.db.service.order.IRegisterOrderService;
import com.health.mapp.bean.Header;
import com.health.mapp.bean.ProcessOrderReq;
import com.health.mapp.bean.ProcessOrderResp;
import com.health.mapp.utils.DateUtils;
import com.health.mapp.utils.Response;
import com.health.mapp.utils.ResultCode;

/**
 * 
 *  订单处理类
 *  用户与视屏以及电话咨询扣取时间
 * @author HiRoad
 *
 */
public class ProcessOrderImpl implements ActionInterface {
	private IRegisterOrderService registerOrderService  ;
	
	@Override
	public Response execute(Header header, Object reqBean) throws Exception {
		
		ProcessOrderReq req=(ProcessOrderReq) reqBean;
		ProcessOrderResp _resp= new ProcessOrderResp();
		
		Long orderId = req.getOrderId();
		String  startTime=req.getStartTime() ;
		String  endTime=req.getEndTime();
		
		if (orderId==0 ||StringUtils.isBlank(startTime)||StringUtils.isBlank(endTime)) {
			_resp.setErrorCode(ResultCode.OPERATION_LAKE_PARAMETER_ORDERID);
			_resp.setErrorMsg(ResultCode.OPERATION_LAKE_PARAMETER_ORDERID_NOTE);
			return _resp;
		}
		
		RegisterOrder  currRegisterOrder =registerOrderService.selectSimpleSingleByOrderId(orderId);
		
		//订单处理bean
		RegisterOrderZProcess registerOrderZProcess = new RegisterOrderZProcess();
		registerOrderZProcess.setStartTime(DateUtils.parseDatetime(startTime));
		registerOrderZProcess.setEndTime(DateUtils.parseDatetime(endTime));
		
		
		//订单错误
		if (null!=currRegisterOrder&&null!=currRegisterOrder.getOrderLeftTime()) {
			
			registerOrderZProcess.setSystemid(0);
			registerOrderZProcess.setRegisterid(currRegisterOrder.getRegisterid());
			registerOrderZProcess.setUserid(currRegisterOrder.getUserid());
			registerOrderZProcess.setAccepterid(currRegisterOrder.getAccepterid());
			
			int betweenSecond = DateUtils.judgmentDateBetween(registerOrderZProcess.getStartTime(), registerOrderZProcess.getEndTime());
			RegisterOrder temp =new RegisterOrder();
			temp.setRegisterid(orderId);
			temp.setOrderLeftTime(currRegisterOrder.getOrderLeftTime().subtract(new BigDecimal(betweenSecond)));
			
			registerOrderService.updateOrder4Precess(temp, registerOrderZProcess);
			
			_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_3007);
			_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_3007_NOTE);
		}else{
			_resp.setErrorCode(ResultCode.OPERATION_ORDER_ERROR_4001);
			_resp.setErrorMsg(ResultCode.OPERATION_ORDER_ERROR_4001_NOTE);
			return _resp;
		}
		
		_resp.setErrorCode(ResultCode.OPERATION_SUCCESS);
		_resp.setErrorMsg(ResultCode.OPERATION_SUCCESS_NOTE);
 
		return _resp;
	}

	public IRegisterOrderService getRegisterOrderService() {
		return registerOrderService;
	}

	public void setRegisterOrderService(IRegisterOrderService registerOrderService) {
		this.registerOrderService = registerOrderService;
	}

}
